package view;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.tv.xlet.XletStateChangeException;

import org.dvb.ui.DVBColor;
import org.havi.ui.HContainer;
import org.havi.ui.HScene;
import org.havi.ui.HTextButton;
import org.havi.ui.event.HRcEvent;

import components.Form;

public class MainMenu extends HContainer implements KeyListener {

    private final HTextButton button_register;

    private HContainer containerAtual;

    private final HContainer containerPadrao;

    private final HScene scene;

    private final XletView xletView;

    private final HTextButton button_remove;

    private final HTextButton button_sel;

    private final HTextButton botao_exit;

    public MainMenu(XletView xletView, int x, int y, int width, int height, HScene scene) {
        super(x, y, width, height);

        this.xletView = xletView;
        this.scene = scene;
        button_register = new CustomizedHTextButton("Incluir dispositivo", 10, 20, 120, 80, new DVBColor(DVBColor.white), new DVBColor(0, 0, 100, 255), new DVBColor(0, 255, 0, 100));
        //button_register = new HTextButton("Incluir dispositivos", 10, 20, 120, 80);
        this.add(button_register);

        button_remove = new CustomizedHTextButton("Excluir dispositivo", 10, 120, 120, 80, new DVBColor(DVBColor.white), new DVBColor(0, 0, 100, 255), new DVBColor(0, 255, 0, 100));
        this.add(button_remove);

        button_sel = new CustomizedHTextButton("Selecionar dispositivo", 10, 220, 120, 80, new DVBColor(DVBColor.white), new DVBColor(0, 0, 100, 255), new DVBColor(0, 255, 0, 100));
        this.add(button_sel);

        botao_exit = new CustomizedHTextButton("Sair", 10, 320, 120, 80, new DVBColor(DVBColor.white), new DVBColor(255, 255, 255, 79), new DVBColor(255, 0, 0, 255));
        this.add(botao_exit);

        button_register.setFocusTraversal(botao_exit, button_remove, null, null);
        button_remove.setFocusTraversal(button_register, button_sel, null, null);
        button_sel.setFocusTraversal(button_remove, botao_exit, null, null);
        botao_exit.setFocusTraversal(button_sel, button_register, null, null);

        button_register.addKeyListener(this);
        button_remove.addKeyListener(this);
        button_sel.addKeyListener(this);
        botao_exit.addKeyListener(this);

        containerPadrao = new CustomizedContainer(0, 450, 715, 120);
        //containerPadrao = new HContainer(0, 450, 715, 120);
        scene.add(containerPadrao);
        containerAtual = containerPadrao;

        button_register.requestFocus();
    }

    public void openFunctionForm(Form form) {
        removePanelScene(containerAtual);
        DynamicRender dynamicRender = new DynamicRender(0, 450, 715, 120, this, form);
        scene.add(dynamicRender);
        scene.repaint();
        dynamicRender.validate();
        dynamicRender.requestFocus();
    }

    public void closeFunctionForm(HContainer container) {
        removePanelScene(container);
        scene.add(containerAtual);
        scene.repaint();
        containerAtual.requestFocus();
    }

    public void keyPressed(KeyEvent e) {
        if (e.getKeyChar() == HRcEvent.VK_ENTER) {
            if (e.getSource() == button_register) {
                showDeviceregister();
            } else if (e.getSource() == button_remove) {
                showDeviceRemove();
            } else if (e.getSource() == button_sel) {
                showDeviceSelect();
            } else if (e.getSource() == botao_exit) {
                scene.requestFocus();
                try {
                    xletView.destroyXlet(true);
                } catch (XletStateChangeException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    private void showDeviceSelect() {
        //scene.remove(containerAtual);
        DeviceSelect deviceSelect = new DeviceSelect(0, 450, 715, 120, this);
        scene.add(deviceSelect);
        scene.repaint();
        deviceSelect.validate();
        containerAtual = deviceSelect;
        deviceSelect.requestFocus();
    }

    private void showDeviceRemove() {
        //scene.remove(containerAtual);
        DeviceRevome deviceRemove = new DeviceRevome(0, 450, 715, 120, this);
        scene.add(deviceRemove);
        scene.repaint();
        deviceRemove.validate();
        containerAtual = deviceRemove;
        containerAtual.requestFocus();
    }

    private void showDeviceregister() {
        //scene.remove(containerAtual);
        DeviceRegister deviceRegister = new DeviceRegister(0, 450, 715, 120, this);
        scene.add(deviceRegister);
        scene.repaint();
        repaint();
        deviceRegister.validate();
        containerAtual = deviceRegister;
        containerAtual.requestFocus();
    }

    public void removePanelScene(HContainer container) {
        //scene.add(container);
        scene.remove(container);
        scene.repaint();
        //containerAtual.requestFocus();
        transferFocus();
    }

    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

    }

    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

    }

    @Override
    public void paint(Graphics g) {
        g.setColor(new DVBColor(0, 0, 164, 255));
        g.fillRect(0, 0, getWidth(), getHeight());
        super.paint(g);
    }

}
